import java.util.*;

public class SystemTest {
  public static void main(String[] args) {
    DataBase dB = new DataBase();
    DomainLogic dL = new DomainLogic(dB);

    System.out.println("Simulating sign in with username \"carlo\" and password \"password\" ...");
    boolean signedIn = dL.logOn("carlo", "password");
    if (signedIn) {System.out.println("Sign in successful!\n");}
    else {System.out.println("Sign in failed!\n");}

    System.out.println("Simulating search for a restaurant serving \"burgers\" at zipcode \"92122\" ...");
    Preferences prefs = new Preferences();
    Vector<Restaurant> restaurants = dL.findRestaurants(Constants.BURGERS, "92122", prefs);
    if (restaurants != null) {
      System.out.println("Found the following restaurants:");
      System.out.println(restaurants.elementAt(0).getName());
      System.out.println("");
    }
    else {System.out.println("No restaurants found!\n");}

    System.out.println("Simulating saving the preferences used to search for " + restaurants.elementAt(0).getName() + " under name \"fast food\" ...");
    UserPreferences uPrefs = new UserPreferences("fast food", "92122", Constants.BURGERS, prefs);
    System.out.println("Preferences saved!\n");

    // addRestaurant
    System.out.println("Simulating adding a restaurant named 3Tier to the database ...");
    dL.addRestaurant("3Tier", "3211 Holiday Court, La Jolla, CA 92037", "92037", Constants.PIZZA, prefs);
    System.out.println("Verifying the addition ...");
    Restaurant tier = dL.getRestaurant("3Tier");
    System.out.println("Restaurant object (" + tier + ") of name \"" + tier.getName() + "\" found!\n");

    // editRestaurant
    System.out.println("Simulating editing restaurant 3Tier from serving PIZZA to serving BURGERS ...");
    dL.editRestaurant("3Tier", "3211 Holiday Court, La Jolla, CA 92037", "92037", Constants.BURGERS, prefs);
    System.out.println("3Tier now serves: " + dL.getRestaurant("3Tier").getFoodType() + "!\n");

    // deleteRestaurant
    System.out.println("Simulating deleting restaurant 3Tier ...");
    Vector<String> restNames = dL.showRestaurantNames();
    System.out.println("List of restaurants before removal:");
    for (int i=0; i < restNames.size(); ++i) {
      System.out.println(restNames.elementAt(i));
    }
    System.out.println("Removing 3Tier ...");
    dL.deleteRestaurant("3Tier");
    System.out.println("List of remaining restaurants:");
    restNames = dL.showRestaurantNames();
    for (int i=0; i < restNames.size(); ++i) {
      System.out.println(restNames.elementAt(i));
    }
    System.out.println("");

    // registerUser
    System.out.println("Simulating registering Bill Clinton with username bclint ...");
    dL.registerUser("bclint", "bclint", null);
    User bclint = dL.getDataBase().getUsers().get("bclint");
    System.out.println("\"bclint\" registered and found (" + bclint + ")!\n");

    // rateRestaurant
    System.out.println("Simulating rating restaurant \"Dominoes\" with a \"5\" ...");
    Restaurant dom = dL.getRestaurant("Dominoes");
    System.out.println("Average rating before this rating: " + dom.getRating());
    dL.rateRestaurant("Dominoes", 5.0);
    dom = dL.getRestaurant("Dominoes");
    System.out.println("Average rating after this rating: " + dom.getRating() + "\n");

    // sortResults
    System.out.println("Simulating sorting restaurants by descending cost (most expensive first) ...");
    System.out.println("Current restaurants and their affordability:");
    HashMap<String, Restaurant> toSort = dL.getDataBase().getRestaurants();
    Vector<Restaurant> toSortVect = new Vector<Restaurant>();
    Restaurant r;
    Set k = toSort.keySet();
    Object[] keySet = k.toArray();
    for (int i=0; i < keySet.length; ++i) {
      r = dL.getRestaurant((String)keySet[i]);
      toSortVect.add(r);
      System.out.println(r.getName() + ": " + r.getPreferences().getCost().elementAt(0));
    }
    Vector<Restaurant> sortedRests = dL.sortResults(toSortVect, "C", "d");
    System.out.println("Restaurants and their affordability after sort:");
    for (int i=0; i < sortedRests.size(); ++i) {
      r = sortedRests.elementAt(i);
      System.out.println(r.getName() + ": " + r.getPreferences().getCost().elementAt(0));
    }
    System.out.println("");
  }

}
